package com.shujia.wyh.mr.pinglun2;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/*
    很好 到位 完美地 不错

    失望 差 太差 不新鲜
 */
public class PingLunMapper extends Mapper<LongWritable, Text, Text, LongWritable> {


    @Override
    protected void setup(Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //这个方法，会在每一个map逻辑之前先执行一遍
        //    /bigdata25/words/words2.txt
        Configuration conf = new Configuration();
        //通过集群配置文件获取HDFS文件系统
        FileSystem fs = FileSystem.get(conf);
        //FSDataInputStream通过观察源码发现，这个类实际上是一个字节流
        FSDataInputStream open = fs.open(new Path("/bigdata25/words/words2.txt"));
        //将字节流包装成字符流
        BufferedReader br = new BufferedReader(new InputStreamReader(open));
        String line = null;
        while ((line=br.readLine())!=null){
            CustomDictionary.add(line);
        }
    }

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {


        //指定自定义词典的路径
//        String CustomDictionary = "src/main/java/com/shujia/wyh/mr/PingLun/words.txt";

//        BufferedReader br = new BufferedReader(new FileReader(CustomDictionary));
//        String w = null;
//        while ((w=br.readLine())!=null){
//            //将自定义词语加入到分词器中
//            com.hankcs.hanlp.dictionary.CustomDictionary.add(w);
//        }
//
//        br.close();
//        CustomDictionary.add("很好");
//        CustomDictionary.add("到位");
//        CustomDictionary.add("完美地");
//        CustomDictionary.add("不错");
//        CustomDictionary.add("失望");
//        CustomDictionary.add("差");
//        CustomDictionary.add("太差");
//        CustomDictionary.add("不新鲜");

        //创建分词器对象
        Segment segment = HanLP.newSegment();


        //public List<Term> seg(String text)
        List<Term> termList = segment.seg(value.toString()); //获取将一句话分词后的List集合

        for (Term term : termList) {
            String word = term.word;
            context.write(new Text(word), new LongWritable(1L));
        }
    }
}
